package com.etl.dataflow.scheduler.admin.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etl.dataflow.common.response.R;
import com.etl.dataflow.common.util.ResponseUtil;
import com.etl.dataflow.scheduler.admin.entity.DataflowExecutorInfo;
import com.etl.dataflow.scheduler.admin.entity.request.PageRequest;
import com.etl.dataflow.scheduler.admin.entity.vo.DataflowExecutorInfoVo;
import com.etl.dataflow.scheduler.admin.mapper.DataflowExecutorInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dx
 * @since 2022/4/21
 */
@Slf4j
@Service
public class ExecutorInfoService extends ServiceImpl<DataflowExecutorInfoMapper, DataflowExecutorInfo> implements DefaultService<DataflowExecutorInfo> {

    private Page<DataflowExecutorInfo> paging(PageRequest pageRequest) {
        return super.baseMapper.selectPage(toPage(pageRequest), orLikeAllFields(pageRequest, DataflowExecutorInfo.class, "idt"));
    }

    public R page(PageRequest pageRequest) {
        Page<DataflowExecutorInfo> page = this.paging(pageRequest);
        if (page.getTotal() <= 0 || page.getRecords().size() <= 0) {
            return R.success().put("data", page);
        }
        List<DataflowExecutorInfo> records = page.getRecords();
        List<DataflowExecutorInfoVo> newRecords = records.parallelStream().map(record -> {
            DataflowExecutorInfoVo vo = new DataflowExecutorInfoVo();
            BeanUtil.copyProperties(record, vo);
            setExecutorRuntimeInfoIgnoreException(vo);
            return vo;
        }).collect(Collectors.toList());
        Page<DataflowExecutorInfoVo> newPage = new Page<>();
        BeanUtil.copyProperties(page, newPage);
        newPage.setRecords(newRecords);
        return R.success().put("data", newPage);
    }

    private void setExecutorRuntimeInfoIgnoreException(DataflowExecutorInfoVo executorInfoVo) {
        try {
            this.setExecutorRuntimeInfo(executorInfoVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setExecutorRuntimeInfo(DataflowExecutorInfoVo executorInfoVo) {
        String url = "http://" + executorInfoVo.getHost() + ":" + executorInfoVo.getPort() + "/getRuntimeInfo";
        String body;
        try (HttpResponse httpResponse = HttpUtil.createGet(url).execute()) {
            body = httpResponse.body();
        }
        JSONObject json = ResponseUtil.toJson(body);
        JSONObject data = json.getJSONObject("data");
        BigDecimal sysCpu = data.getBigDecimal("sysCpu");
        BigDecimal totalMemory = data.getBigDecimal("totalMemory");
        BigDecimal freeMemory = data.getBigDecimal("freeMemory");
        Integer taskNumber = data.getInt("taskNumber");
        BigDecimal usedMemory = totalMemory.subtract(freeMemory);
        executorInfoVo.setCpuUsage(sysCpu);
        executorInfoVo.setTotalMemory(totalMemory);
        executorInfoVo.setUsedMemory(usedMemory);
        executorInfoVo.setTaskNumber(taskNumber);
    }

}
